package j20251025;

import java.util.concurrent.*;

public class Demo1 {
    public static void main(String[] args) {
        // 创建固定大小的线程池
        ExecutorService executor = Executors.newFixedThreadPool(2);

        // 提交Callable任务
        Future<String> future1 = executor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(1000);
                return "Callable任务执行完成";
            }
        });

        // 提交Runnable任务并指定返回值
        Future<String> future2 = executor.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("Runnable任务正在执行...");
            }
        }, "Runnable任务执行完成");

        try {
            // 获取Callable任务结果
            String result1 = future1.get();
            System.out.println("Callable任务结果: " + result1);

            // 获取Runnable任务结果
            String result2 = future2.get();
            System.out.println("Runnable任务结果: " + result2);

        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        } finally {
            // 关闭线程池
            executor.shutdown();
        }
    }
}
